//
//  CourseManagerCell.m
//  TrainTree
//
//  Created by wanghui on 2017/9/19.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "CourseManagerCell.h"
 
@implementation CourseManagerCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.namesArray = @[@"海明威",@"林语堂",@"蔡元培",@"莫泊桑",@"欧亨利"];
    self.typeBtn.layer.cornerRadius = 18.0f;
    self.typeBtn.layer.masksToBounds = YES;

}

- (void)configWithData:(NSDictionary *)dic
{
    int iconNum = [dic[@"themeId"] intValue];

    NSString *themeId = [NSString stringWithFormat:@"%d",iconNum % 6];
    [self.typeBtn setImage:ImageNamed([KindTurnIcon iconWithId:themeId]) forState:UIControlStateNormal];


    id error = dic[@"moduleName"];
    self.leftTitle.text = [error stringValue];
    self.rightTitle.text = [NSString stringWithFormat:@" / %@ ",dic[@"lessonName"]];
    self.time.text = [NSString stringWithFormat:@"%@ 剩余%@天",dic[@"finishTime"],dic[@"overDays"]];
    NSNumber *memberCount = dic[@"memberCount"];
    self.nameNumber.text = [NSString stringWithFormat:@"...%@+",[memberCount stringValue]];
    self.namesArray = dic[@"members"];

    if (self.namesArray.count >= 4) {
        self.nameNumber.hidden = NO;
    }else{
        self.nameNumber.hidden = YES;
    }

    int margin = 8;
    if ([UIDevice isiPhone6]) {
        margin = 10;
    } else if ([UIDevice isPlus]) {
        margin = 15;
    } else {
        margin = 7;
    }

    if (self.namesArray.count >= 1) {
        NSString *str0 = self.namesArray[0];
        CGFloat wid0 = [self widthForLabel:str0 fontSize:10];
        UILabel *lab0 = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, wid0, 16)];
        [self setLabelProperty:lab0 withStr:str0];

        if (self.namesArray.count >= 2) {
            NSString *str1 = self.namesArray[1];
            CGFloat wid1 = [self widthForLabel:str1 fontSize:10];
            UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(wid0 + margin, 3, wid1, 16)];
            [self setLabelProperty:lab1 withStr:str1];

            if (self.namesArray.count >= 3) {
                NSString *str2 = self.namesArray[2];
                CGFloat wid2 = [self widthForLabel:str2 fontSize:10];
                UILabel *lab2 = [[UILabel alloc]initWithFrame:CGRectMake(wid0 + margin + wid1 + margin, 3, wid2, 16)];
                [self setLabelProperty:lab2 withStr:str2];

                if (self.namesArray.count >= 4) {
                    NSString *str3 = self.namesArray[3];
                    CGFloat wid3 = [self widthForLabel:str3 fontSize:10];
                    UILabel *lab3 = [[UILabel alloc]initWithFrame:CGRectMake(wid0 + margin + wid1 + margin + wid2 + margin, 3, wid3, 16)];
                    [self setLabelProperty:lab3 withStr:str3];

                    if (self.namesArray.count >= 5) {
                        NSString *str4 = self.namesArray[4];
                        CGFloat wid4 = [self widthForLabel:str4 fontSize:10];
                        UILabel *lab4 = [[UILabel alloc]initWithFrame:CGRectMake(wid0 + margin + wid1 + margin + wid2 + margin + wid3 + margin, 3, wid4, 16)];
                        [self setLabelProperty:lab4 withStr:str4];
                    }
                }
            }
        }
    }
}

- (void)setLabelProperty:(UILabel *)lab withStr:(NSString *)str
{
    lab.text = str;
    lab.textColor = rgb(164.0, 164.0, 164.0);
    lab.textAlignment = NSTextAlignmentCenter;
    [lab setFont:[UIFont systemFontOfSize:10]];
    lab.layer.cornerRadius = 8.0f;
    lab.layer.masksToBounds = YES;
    lab.layer.borderWidth = 1.0f;
    lab.layer.borderColor = rgb(216, 216, 216).CGColor;
    [self.nameView addSubview:lab];
}

- (CGFloat )widthForLabel:(NSString *)text fontSize:(CGFloat)font
{
    CGSize size = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font], NSFontAttributeName, nil]];
    if ([UIDevice isiPhone6]) {
        return size.width + 12 ;
    } else if ([UIDevice isPlus]) {
        return size.width + 15 ;
    } else {
        return size.width + 5 ;
    }

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
